﻿/*
* ---------------------------------------------------
* 代码自动生成  请不要手动修改代码
* 作者: MTQ 
* 日期： 2016/1/3 15:26:16 
* ---------------------------------------------------
*/
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MTQ.Model;
using MTQ.BLL;
using MTQ.IBLL;
using MTQ.Common;

namespace MTQ.Web.Controllers
{

    public class Base_UserInfoController : BaseController
    {

        private IBase_UserInfoService iBase_userinfoService = new Base_UserInfoService();


        //                                           
        // GET: //         
        [AccountAuth(Roles = "Base_UserInfo_Index")]
        public ActionResult Index()
        {
            return View(iBase_userinfoService.LoadEntities(x => true).ToList());
        }


        [AccountAuth(Roles = "Base_UserInfo_Index")]
        public ActionResult Search()
        {

            Base_UserInfoQuery Base_userinfoQuery = new Base_UserInfoQuery();

            ServertBootStrapTable servertBootStrapTable = new ServertBootStrapTable();

            servertBootStrapTable = iBase_userinfoService.LoadBootstrapSearchData(Base_userinfoQuery, Request);

            return Json(servertBootStrapTable, JsonRequestBehavior.AllowGet);
        }


        //          
        // GET: /Base_UserInfo/Create          
        [AccountAuth(Roles = "Base_UserInfo_Create")]
        public ActionResult Create()
        {
            Base_UserInfo Base_userinfo = new Base_UserInfo();
            Base_userinfo.User_Id = System.Guid.NewGuid();

            return View(Base_userinfo);
        }

        //          
        // POST: /Base_UserInfo/Create          
        [HttpPost]
        [AccountAuth(Roles = "Base_UserInfo_Create")]
        public ActionResult Create(Base_UserInfo Base_userinfo)
        {
            List<ModelMessage> listMsg = new List<ModelMessage>();
            if (ModelState.IsValid)
            {
                var result = iBase_userinfoService.AddEntity(Base_userinfo);
                listMsg.Add(new ModelMessage("ChangeResult", "m001", result != null ? "保存成功" : "保存失败"));

            }

            return Json(listMsg);
        }




        // POST: /Base_UserInfo/Create                                                    
        [AccountAuth(Roles = "Base_UserInfo_Edit")]
        public ActionResult Edit(Guid? primarykey)
        {
            Base_UserInfo Base_userinfo = iBase_userinfoService.LoadEntities(x => x.User_Id == primarykey).FirstOrDefault();
            if (Base_userinfo == null)
            {
                return HttpNotFound();
            }
            return View(Base_userinfo);
        }

        [AccountAuth(Roles = "Base_UserInfo_ModifyPassword")]
        public ActionResult ModifyPassword(Guid? primarykey)
        {
            Base_UserInfo Base_userinfo = iBase_userinfoService.LoadEntities(x => x.User_Id == primarykey).FirstOrDefault();
            if (Base_userinfo == null)
            {
                return HttpNotFound();
            }
            Base_userinfo.User_Password = "";
            return View(Base_userinfo);
        }
        [HttpPost]
        [AccountAuth(Roles = "Base_UserInfo_Edit")]
        public ActionResult ModifyPassword(Base_UserInfo userinfo)
        {

            List<ModelMessage> listMsg = new List<ModelMessage>();
            string password_Old = Request["Base_UserInfoPassword_Old"].ToString();
            string password = Request["Base_UserInfoPassword"].ToString();
            string password_Again = Request["Base_UserInfoPassword_Again"].ToString();

            if (password != password_Again)
            {
                listMsg.Add(new ModelMessage("ChangeResult", "m002", "两次密码不一致"));
                return Json(listMsg);
            }


            Base_UserInfo Base_userinfo = new Base_UserInfoService().LoadEntities(x => x.User_Id == userinfo.User_Id).FirstOrDefault();
            if (Base_userinfo.User_Password != MD5Cryptography.MD5Encrypt(password_Old))
            {
                listMsg.Add(new ModelMessage("ChangeResult", "m002", "原密码不正确"));
                return Json(listMsg);
            }

            try
            {
                Base_userinfo.User_Password = MD5Cryptography.MD5Encrypt(password);
                bool result = iBase_userinfoService.UpdateEntity(Base_userinfo);
                listMsg.Add(new ModelMessage("ChangeResult", "m001", result ? "保存成功" : "保存失败"));
            }
            catch (Exception e)
            {
                listMsg.Add(new ModelMessage("ChangeResult", "m002", "保存失败"));
            }

            return Json(listMsg);

        }

        [HttpPost]
        [AccountAuth(Roles = "Base_UserInfo_Edit")]
        public ActionResult Edit(Base_UserInfo Base_userinfo)
        {
            List<ModelMessage> listMsg = new List<ModelMessage>();
            if (ModelState.IsValid)
            {
                try
                {
                    bool result = iBase_userinfoService.UpdateEntity(Base_userinfo);
                    listMsg.Add(new ModelMessage("ChangeResult", "m001", result ? "保存成功" : "保存失败"));
                }
                catch (Exception e)
                {
                    listMsg.Add(new ModelMessage("ChangeResult", "m002", "保存失败"));
                }
            }
            return Json(listMsg);

        }


        [HttpPost]
        [AccountAuth(Roles = "Base_UserInfo_Edit")]
        public ActionResult ActivateBase_UserInfo(string primarykeys, string activate = "N")
        {
            ModelMessage modelMessage = new ModelMessage();

            if (OperatorAvailable(primarykeys, activate))
            {
                modelMessage.Code = "m001";
                modelMessage.Message = "修改成功";
            }
            else
            {
                modelMessage.Code = "m002";
            }
            return Json(modelMessage, JsonRequestBehavior.AllowGet);
        }


        private bool OperatorAvailable(string primarykeys, string available)
        {
            List<Guid> listBase_UserInfoGUID = primarykeys.Split(',').ToListGuid();
            List<Base_UserInfo> listBase_UserInfo = iBase_userinfoService.LoadEntities(x => listBase_UserInfoGUID.Contains(x.User_Id)).ToList();
            foreach (var item in listBase_UserInfo)
            {
                item.User_Available = available;
            }
            int updaterow = iBase_userinfoService.UpdateEntity();
            if (updaterow == 0)
            {
                return false;
            }
            return true;
        }



    }
}


